package org.nyt.medicalstore.repository;

import org.nyt.medicalstore.model.Drug;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface DrugRepository extends JpaRepository<Drug, Long> {
    // 根据描述或名称搜索药品
    List<Drug> findByDescriptionContainingOrNameContaining(String description, String name);
    
    // 根据名称搜索药品
    List<Drug> findByNameContaining(String name);
    
    // 根据描述搜索药品
    List<Drug> findByDescriptionContaining(String description);
    
    // 根据规格或剂型搜索药品
    List<Drug> findBySpecificationContainingOrDosageFormContaining(String specification, String dosageForm);

    //查找相似药品
    @Query("SELECT ds.drug2 FROM DrugSimilarity ds WHERE ds.drug1.id = :drugId AND ds.similarity >= :minScore " +
            "UNION " +
            "SELECT ds.drug1 FROM DrugSimilarity ds WHERE ds.drug2.id = :drugId AND ds.similarity >= :minScore")
    List<Drug> findSimilarDrugs(@Param("drugId") Long drugId, @Param("minScore") Double minScore);
}

